#How Chatbot Communication Styles Impact Citizen Reports to Police: Testing procedural justice and overaccommodation approaches in a survey experiment
##Coding Document
###Callie Vitro

#Import dataset
library(rio)
library(dplyr)
library(tidyverse)
library(lsr)
library(stargazer)
library(lmtest)
library(sandwich)

data<-import("ChatbotCommunication_Data.xlsx")
cols<-c("racew_bin", "man", "cb_familiar", "cb_interacted", "contact_cb")
data[cols] <- lapply(data[cols], factor) 

#Descriptives - Tables 1 & A1

library(JWileymisc)
table<-egltable(c("man","age", "racew_bin", "ideology", "edu", "income", "individual_acc", "situation_acc", "contact_survey", "contact_cb", "trust", "cb_messagecount", "cb_familiar", "cb_interacted"), data = data)
df.table <- as.data.frame(table)
View(df.table)
colnames(df.table)<- c("Item", "M(SD)/N(%)")


##HIGH POWER HIGH SOLIDARITY MODELS

#################################################################################
#Solidarity Only Models - Tables 2, S4, & S7

##Table S4 (Constrained)

###Appendix OLS Model 1
summary(HS_acc_indiv0<-lm(individual_acc ~ high_solidarity, data=data))
HS_acc_indiv0.<-coeftest(HS_acc_indiv0, vcov = vcovHC(HS_acc_indiv0, type="HC1"))
HS_acc_indiv0.
stargazer(HS_acc_indiv0., type = "text", ci=T)

###Appendix OLS Model 2
summary(HS_acc_sit0<-lm(situation_acc ~ high_solidarity, data=data))
HS_acc_sit0.<-coeftest(HS_acc_sit0, vcov = vcovHC(HS_acc_sit0, type="HC1"))
HS_acc_sit0.
stargazer(HS_acc_sit0., type = "text", ci=T)

###Appendix Binary Logistic Model 1
summary(HS_CPA_Bin0<-glm(contact_cb ~ high_solidarity, data = data, family = binomial()))
DescTools::PseudoR2(HS_CPA_Bin0, which = "all")
HS_CPA_Bin0.<-coeftest(HS_CPA_Bin0, vcov = vcovHC(HS_CPA_Bin0, type="HC1"))
HS_CPA_Bin0.
stargazer(HS_CPA_Bin0., type = "text", ci=T)

###Appendix OLS Model 3
summary(HS_Trust0<-lm(trust ~ high_solidarity, data=data))
HS_Trust0.<-coeftest(HS_Trust0, vcov = vcovHC(HS_Trust0, type="HC1"))
HS_Trust0.
stargazer(HS_Trust0., type = "text", ci=T)

##Table 2 (Full)

###OLS Model 1
summary(HS_acc_indiv1<-lm(individual_acc ~ high_solidarity + man + age + racew_bin +  ideology + edu + income + cb_messagecount + cb_familiar + cb_interacted, data=data))
HS_acc_indiv1.<-coeftest(HS_acc_indiv1, vcov = vcovHC(HS_acc_indiv1, type="HC1"))
HS_acc_indiv1.
stargazer(HS_acc_indiv1., type = "text", ci=T)

###OLS Model 2
summary(HS_acc_sit1<-lm(situation_acc ~ high_solidarity + man + age + racew_bin +  ideology + edu + income + cb_messagecount + cb_familiar + cb_interacted, data=data))
HS_acc_sit1.<-coeftest(HS_acc_sit1, vcov = vcovHC(HS_acc_sit1, type="HC1"))
HS_acc_sit1.
stargazer(HS_acc_sit1., type = "text", ci=T)

###Binary Logistic Model 1
summary(HS_CPA_Bin1<-glm(contact_cb ~ high_solidarity+ man + age + racew_bin +  ideology + edu + income + cb_messagecount + cb_familiar + cb_interacted, data = data, family = binomial()))
DescTools::PseudoR2(HS_CPA_Bin1, which = "all")
HS_CPA_Bin1.<-coeftest(HS_CPA_Bin1, vcov = vcovHC(HS_CPA_Bin1, type="HC1"))
HS_CPA_Bin1.
stargazer(HS_CPA_Bin1., type = "text", ci=T)

###OLS Model 3
summary(HS_Trust1<-lm(trust ~ high_solidarity + man + age + racew_bin + ideology + edu + income + cb_messagecount + cb_familiar + cb_interacted, data=data))
HS_Trust1.<-coeftest(HS_Trust1, vcov = vcovHC(HS_Trust1, type="HC1"))
HS_Trust1.
stargazer(HS_Trust1., type = "text",ci=T)

##Table S7 (Alternate (survey) coding of willingness to provide information)

###Appendix OLS Model 10
summary(HS_CPA_OLS0<-lm(contact_survey ~ high_solidarity, data=data))
HS_CPA_OLS0.<-coeftest(HS_CPA_OLS0, vcov = vcovHC(HS_CPA_OLS0, type="HC1"))
stargazer(HS_CPA_OLS0., type = "text", ci=T)

###Appendix OLS Model 11
summary(HS_CPA_OLS1<-lm(contact_survey ~ high_solidarity + man + age + racew_bin +  ideology + edu + income + cb_messagecount + cb_familiar + cb_interacted, data=data))
HS_CPA_OLS1.<-coeftest(HS_CPA_OLS1, vcov = vcovHC(HS_CPA_OLS1, type="HC1"))
stargazer(HS_CPA_OLS1., type = "text", ci=T)

#######################################################################
#Power Only Models - Tables 3, S5, & S8

##Table S5 (Constrained)

###Appendix OLS Model 4
summary(HP_acc_indiv0<-lm(individual_acc ~ high_power, data=data))
HP_acc_indiv0.<-coeftest(HP_acc_indiv0, vcov = vcovHC(HP_acc_indiv0, type="HC1"))
stargazer(HP_acc_indiv0., type = "text", ci=T)

###Appendix OLS Model 5
summary(HP_acc_sit0<-lm(situation_acc ~ high_power, data=data))
HP_acc_sit0.<-coeftest(HP_acc_sit0, vcov = vcovHC(HP_acc_sit0, type="HC1"))
stargazer(HP_acc_sit0., type = "text", ci=T)

###Appendix Binary Logistic Model 2
summary(HP_CPA_Bin0<-glm(contact_cb ~ high_power, data = data, family = binomial()))
DescTools::PseudoR2(HP_CPA_Bin0, which = "all")
HP_CPA_Bin0.<-coeftest(HP_CPA_Bin0, vcov = vcovHC(HP_CPA_Bin0, type="HC1"))
stargazer(HP_CPA_Bin0., type = "text", ci=T)

###Appendix OLS Model 6
summary(HP_Trust0<-lm(trust ~ high_power, data=data))
HP_Trust0.<-coeftest(HP_Trust0, vcov = vcovHC(HP_Trust0, type="HC1"))
stargazer(HP_Trust0., type = "text", ci=T)

##Table 3 (Full)

###OLS Model 4
summary(HP_acc_indiv1<-lm(individual_acc ~ high_power + man + age + racew_bin +  ideology + edu + income + cb_messagecount + cb_familiar + cb_interacted, data=data))
HP_acc_indiv1.<-coeftest(HP_acc_indiv1, vcov = vcovHC(HP_acc_indiv1, type="HC1"))
stargazer(HP_acc_indiv1., type = "text", ci=T)

###OLS Model 5
summary(HP_acc_sit1<-lm(situation_acc ~ high_power + man + age + racew_bin +  ideology + edu + income + cb_messagecount + cb_familiar + cb_interacted, data=data))
HP_acc_sit1.<-coeftest(HP_acc_sit1, vcov = vcovHC(HP_acc_sit1, type="HC1"))
stargazer(HP_acc_sit1., type = "text", ci=T)

###Binary Logistic Model 2
summary(HP_CPA_Bin1<-glm(contact_cb ~ high_power+ man + age + racew_bin +  ideology + edu + income + cb_messagecount + cb_familiar + cb_interacted, data = data, family = binomial()))
DescTools::PseudoR2(HP_CPA_Bin1, which = "all")
HP_CPA_Bin1.<-coeftest(HP_CPA_Bin1, vcov = vcovHC(HP_CPA_Bin1, type="HC1"))
stargazer(HP_CPA_Bin1., type = "text", ci=T)

###OLS Model 6
summary(HP_Trust1<-lm(trust ~ high_power + man + age + racew_bin + ideology + edu + income+cb_messagecount + cb_familiar + cb_interacted,
                 data=data))
HP_Trust1.<-coeftest(HP_Trust1, vcov = vcovHC(HP_Trust1, type="HC1"))
stargazer(HP_Trust1., type = "text",ci=T)

##Table S8 (Alternate (survey) coding of willingness to provide information)

###Appendix OLS Model 12
summary(HP_CPA_OLS0<-lm(contact_survey ~ high_power, data=data))
HP_CPA_OLS0.<-coeftest(HP_CPA_OLS0, vcov = vcovHC(HP_CPA_OLS0, type="HC1"))
stargazer(HP_CPA_OLS0., type = "text", ci=T)

###Appendix OLS Model 13
summary(HP_CPA_OLS1<-lm(contact_survey ~ high_power + man + age + racew_bin +  ideology + edu + income + cb_messagecount + cb_familiar + cb_interacted,
               data=data))
HP_CPA_OLS1.<-coeftest(HP_CPA_OLS1, vcov = vcovHC(HP_CPA_OLS1, type="HC1"))
stargazer(HP_CPA_OLS1., type = "text", ci=T)

#######################################################################
#All Condition Models - Tables 4, S6, & S9

##Table S6 (Constrained)

###Appendix OLS Model 7
summary(acc_indiv0<-lm(individual_acc ~ condition, data=data))
acc_indiv0.<-coeftest(acc_indiv0, vcov = vcovHC(acc_indiv0, type="HC1"))
stargazer(acc_indiv0., type = "text", ci=T)

###Appendix OLS Model 8
summary(acc_sit0<-lm(situation_acc ~ condition, data=data))
acc_sit0.<-coeftest(acc_sit0, vcov = vcovHC(acc_sit0, type="HC1"))
stargazer(acc_sit0., type = "text", ci=T)

###Appendix Binary Logistic Model 3
summary(CPA_Bin0<-glm(contact_cb ~ condition, data = data, family = binomial()))
DescTools::PseudoR2(CPA_Bin0, which = "all")
CPA_Bin0.<-coeftest(CPA_Bin0, vcov = vcovHC(CPA_Bin0, type="HC1"))
stargazer(CPA_Bin0., type = "text", ci=T)

###Appendix OLS Model 9
summary(Trust0<-lm(trust ~ condition,
                   data=data))
Trust0.<-coeftest(Trust0, vcov = vcovHC(Trust0, type="HC1"))
stargazer(Trust0., type = "text", ci=T)

##Table 4 (Full)

###OLS Model 7
#Full model
summary(acc_indiv1<-lm(individual_acc ~ condition + man + age + racew_bin +  ideology + edu + income + cb_messagecount + cb_familiar + cb_interacted, data=data))
acc_indiv1.<-coeftest(acc_indiv1, vcov = vcovHC(acc_indiv1, type="HC1"))
stargazer(acc_indiv1., type = "text", ci=T)

###OLS Model 8
summary(acc_sit1<-lm(situation_acc ~ condition + man + age + racew_bin +  ideology + edu + income + cb_messagecount + cb_familiar + cb_interacted, data=data))
acc_sit1.<-coeftest(acc_sit1, vcov = vcovHC(acc_sit1, type="HC1"))
stargazer(acc_sit1., type = "text", ci=T)

###Binary Logistic Model 3
summary(CPA_Bin1<-glm(contact_cb ~ condition + man + age + racew_bin +  ideology + edu + income + cb_messagecount + cb_familiar + cb_interacted, data = data, family = binomial()))
DescTools::PseudoR2(CPA_Bin1, which = "all")
CPA_Bin1.<-coeftest(CPA_Bin1, vcov = vcovHC(CPA_Bin1, type="HC1"))
stargazer(CPA_Bin1., type = "text", ci=T)

###OLS Model 9
summary(Trust1<-lm(trust ~ condition + man + age + racew_bin + ideology + edu + income+cb_messagecount + cb_familiar + cb_interacted,
              data=data))
Trust1.<-coeftest(Trust1, vcov = vcovHC(Trust1, type="HC1"))
stargazer(Trust1., type = "text",ci=T)

##Table S9 (Alternate (survey) coding of willingness to provide information)

###Appendix OLS Model 14
summary(CPA_OLS0<-lm(contact_survey ~ condition,
            data=data))
CPA_OLS0.<-coeftest(CPA_OLS0, vcov = vcovHC(CPA_OLS0, type="HC1"))
stargazer(CPA_OLS0., type = "text", ci=T)

###Appendix OLS Model 15
summary(CPA_OLS1<-lm(contact_survey ~ condition + man + age + racew_bin +  ideology + edu + income + cb_messagecount + cb_familiar + cb_interacted,
            data=data))
CPA_OLS1.<-coeftest(CPA_OLS1, vcov = vcovHC(CPA_OLS1, type="HC1"))
stargazer(CPA_OLS1., type = "text", ci=T)

#######################################################################
#Non-parametric Tests
library(gvlma)
library(FSA)
library(rstatix)
library(rcompanion)

##individual_acc Description
###Solidarity
wilcox.test(individual_acc ~ high_solidarity, data = data,
            exact = FALSE)
wilcoxonR(x  = data$individual_acc,
          g  = data$high_solidarity,
          ci = TRUE)

###Power
wilcox.test(individual_acc ~ high_power, data = data,
            exact = FALSE)

wilcoxonR(x  = data$individual_acc,
          g  = data$high_power,
          ci = TRUE)

###All Conditions
kruskal.test(individual_acc ~ condition,
             data=data)
kruskal_effsize(
   data = data,
   formula = individual_acc~condition,
   ci = T,
   conf.level = 0.95,
   ci.type = "perc"
)
dunnTest(individual_acc ~ condition,kw=T,
         data=data,
         method="bonferroni")

##Incident Description
###Solidarity
wilcox.test(situation_acc ~ high_solidarity, data = data,
            exact = FALSE)
wilcoxonR(x  = data$situation_acc,
          g  = data$high_solidarity,
          ci = TRUE)

###Power
wilcox.test(situation_acc ~ high_power, data = data,
            exact = FALSE)
wilcoxonR(x  = data$situation_acc,
          g  = data$high_power,
          ci = TRUE)

###All Conditions
kruskal.test(situation_acc ~ condition,
             data=data)
kruskal_effsize(
   data = data,
   formula = situation_acc~condition,
   ci = T,
   conf.level = 0.95,
   ci.type = "perc"
)
dunnTest(situation_acc ~ condition,kw=T,
         data=data,
         method="bonferroni")

##Willingness to Provide Contact Information
###Solidarity
wilcox.test(contact_survey ~ high_solidarity, data = data,
            exact = FALSE)
wilcoxonR(x  = data$contact_survey,
          g  = data$high_solidarity,
          ci = TRUE)

###Power
wilcox.test(contact_survey ~ high_power, data = data,
            exact = FALSE)
wilcoxonR(x  = data$contact_survey,
          g  = data$high_power,
          ci = TRUE)

###All Conditions
kruskal.test(contact_survey ~ condition,
             data=data)
kruskal_effsize(
   data = data,
   formula = contact_survey~condition,
   ci = T,
   conf.level = 0.95,
   ci.type = "perc"
)
dunnTest(contact_survey ~ condition,kw=T,
         data=data,
         method="bonferroni")

##Trust in the Chatbot
###Solidarity
wilcox.test(trust ~ high_solidarity, data = data,
            exact = FALSE)

wilcoxonR(x  = data$trust,
          g  = data$high_solidarity,
          ci = TRUE)

###Power
wilcox.test(trust ~ high_power, data = data,
            exact = FALSE)
wilcoxonR(x  = data$trust,
          g  = data$high_power,
          ci = TRUE)

###All Conditions
kruskal.test(trust ~ condition,
             data=data)
kruskal_effsize(
   data = data,
   formula = trust~condition,
   ci = T,
   conf.level = 0.95,
   ci.type = "perc"
)


#######################################################################
#Assumptions

##HP-LS Removal 

remove_condition<-data
remove_condition<-filter(remove_condition, condition != "HP LS")

###Individual Accuracy
summary(rem_acc_indiv1<-lm(individual_acc ~ condition + man + age + racew_bin +  ideology + edu + income + cb_messagecount + cb_familiar + cb_interacted, data=remove_condition))
rem_acc_indiv1.<-coeftest(rem_acc_indiv1, vcov = vcovHC(rem_acc_indiv1, type="HC1"))
rem_acc_indiv1.
stargazer(rem_acc_indiv1., type = "text", ci=T)

###Incident Accuracy
summary(rem_acc_sit1<-lm(situation_acc ~ condition + man + age + racew_bin +  ideology + edu + income + cb_messagecount + cb_familiar + cb_interacted, data=remove_condition))
rem_acc_sit1.<-coeftest(rem_acc_sit1, vcov = vcovHC(rem_acc_sit1, type="HC1"))
rem_acc_sit1.
stargazer(rem_acc_sit1., type = "text", ci=T)

###Willingness to Provide Contact Information (Chatbot)
summary(rem_CPA_Bin1<-glm(contact_cb ~ condition + man + age + racew_bin +  ideology + edu + income + cb_messagecount + cb_familiar + cb_interacted, data = remove_condition, family = binomial()))
DescTools::PseudoR2(rem_CPA_Bin1, which = "all")
rem_CPA_Bin1.<-coeftest(rem_CPA_Bin1, vcov = vcovHC(rem_CPA_Bin1, type="HC1"))
rem_CPA_Bin1.
stargazer(rem_CPA_Bin1, type = "text", ci=T)

###Willingness to Provide Contact Information (Survey)
summary(rem_CPA_OLS1<-lm(contact_survey ~ condition + man + age + racew_bin +  ideology + edu + income + cb_messagecount + cb_familiar + cb_interacted,
            data=remove_condition))
rem_CPA_OLS1.<-coeftest(rem_CPA_OLS1, vcov = vcovHC(rem_CPA_OLS1, type="HC1"))
rem_CPA_OLS1.
stargazer(rem_CPA_OLS1., type = "text", ci=T)

###Trust in Chatbot System
summary(rem_Trust1<-lm(trust ~ condition + man + age + racew_bin + ideology + edu + income+cb_messagecount + cb_familiar + cb_interacted,
              data=remove_condition))
rem_Trust1.<-coeftest(rem_Trust1, vcov = vcovHC(rem_Trust1, type="HC1"))
rem_Trust1.
stargazer(rem_Trust1., type = "text",ci=T)

##Chatbot Response Count

###Between conditions overall
kruskal.test(cb_messagecount ~ condition,
             data=data)
kruskal_effsize(
   data = data,
   formula = cb_messagecount ~ condition,
   ci = T,
   conf.level = 0.95,
   ci.type = "perc"
)

###Between conditions based on full/partial treatment
botmessage_full<-as.factor(ifelse((data$cb_messagecount >= 7), 1 ,0))
prop.table(summary(botmessage_full))
chisq_test(botmessage_full, data$condition)
